﻿$(document).ready(function () {
    // Initialize calendar
    $('#calendar').fullCalendar({
        editable: true,
        allDayDefault: false,
        events: [],
        dayClick: function (date, allDay) {
            var dateString = $.fullCalendar.formatDate(date, 'MM/dd/yyyy hh:mm tt'); 

            // default end date to + 1 hour from the start when clicking on a day 
            var endDate = new Date(date.getTime() + (1 * 60 * 60 * 1000) );
            var endDateString = $.fullCalendar.formatDate(endDate, 'MM/dd/yyyy hh:mm tt');

            if (dateString) {
                dateString = dateString.toString('MM/dd/yyyy hh:mm tt');
            }
            else {
                // Default to today if there was an issue parsing the provided date
                dateString = Date.today().toString('MM/dd/yyyy hh:mm tt');
            }

            $('#create-event-popup').dialog("open");
            $('#start-time').val(dateString);
            $('#end-time').val(endDateString);

        },
        eventClick: function (event) {
            alert("Event " + event.title + " was selected.");

            // Don't follow URLs
            return false;
        }

    });


    // Create date-time pickers for the create-event form
    $('#start-time').datetimepicker({
        ampm: true
    });
    $('#end-time').datetimepicker({
        ampm: true
    });


    // Setup create button on click
    $('#create-event').click(function () {
        $('#create-event-popup').dialog("open");

    });


    // Submit for new event button in create-event form
    $('#submit-event').click(function () {
        var name = $('#event-name').val();
        var startTime = $('#start-time').val();
        var endTime = $('#end-time').val();

        // alert("Name: " + name + " start: " + startTime + " end: " + endTime);
        createEvent(name, startTime, endTime);
        resetForm();
    });


    $('#cancel-event').click(function () {
        resetForm();
    });



    $("#create-event-popup").dialog();
    $("#create-event-popup").dialog("close");



});

function facebookReady() {
    $(document).ready(function () {
        var accessToken = $("#authToken").val();

        /* Get FB events */
        FB.api('/me/events?access_token=' + accessToken, function (response) {
            myEvents = response;
            for (var e in myEvents.data) {
                console.log("adding: " + myEvents.data[e].name);
                addEvent(myEvents.data[e].name, myEvents.data[e].start_time, myEvents.data[e].end_time, false);

            }

        });
    });
}

// Adds event to Facebook
function createEvent(title, start, end, location){
    var sDate = Date.parse(start);
    var eDate = Date.parse(end);
    var now = Date.today();


        FB.api('/me/events', 'post', { name: title, start_time: start, end_time: end, location: location }, function (response) {
            console.log("Create Event Response: " + JSON.stringify(response));

            if (!response.error) {
                addEvent(title, start, end);
            } 
            else {
                // some other error occured.  Don't add the event to the calendar
                alert("Could not create event. " + response.error.message);
            }

        });

}

// Puts event on calendar
function addEvent(title, start, end){
        $('#calendar').fullCalendar('renderEvent', 
                        {
                            title : title,
                            start : start,
                            end   : end
                            
                            
                        },
                        true);
}

// Clear the form to create an element
function resetForm() {
        $('#event-name').val("");
        var startTime = $('#start-time').val("");
        var endTime = $('#end-time').val("");
        $("#create-event-popup").dialog("close");
}